package com.lanou.service;

import com.lanou.dao.InsertDao;
import com.lanou.dao.SelectDao;
import com.lanou.pojo.*;
import com.lanou.utils.AjaxResult;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SelectService {
    private SelectDao selectDao = new SelectDao();
    private InsertDao insertDao = new InsertDao();
    //登录
    public Tb_user selectByusername(String username, String password) {
        try {
            Tb_user tbUser = new Tb_user();
            tbUser.setUsername(username);
            Long aLong = insertDao.selectUserByusername(tbUser);
            if (aLong!=0){
                Tb_user tb_user = selectDao.selectByusername(username,password);
                return tb_user;
            }else {
                return null;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //查询种类
    public List selectCategory() {
        try {
            List category = selectDao.selectCategory();
            if (category!=null){
                return category;
            }else {
                return new ArrayList();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new ArrayList();
    }
    //通过id查询单个类名
    public String selectCateById(Integer cid) {
        System.out.println("selectCateById+"+cid);
        try {
            String cname = selectDao.selectCateById(cid);
            if (cname!=null){
                return cname;
            }else {
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //查询图书
    public List<Book> selectBook() {
        try {
            List<Book> books = selectDao.selectBook();
            if (books!=null){
                return books;
            }else {
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //搜索单本图书和对应类型
    public AjaxResult selectBookAndCate(int bid) {
        try {
            Book book = insertDao.selectBookBybid(bid);
            Object cname = insertDao.selectCateCnameBycid(book.getCid());
            book.setCname((String) cname);
            List category = selectDao.selectCategory();
            AjaxResult ar = new AjaxResult();
            System.out.println(category);
            System.out.println(book);
            ar.setCategory(category);
            ar.setData(book);
            ar.setCode(1000);
            ar.setMsg("查询成功");
            return ar;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //搜索种类和用户名(已登录)
    public AjaxResult selectCateAndusername(int id) {
        AjaxResult ar = new AjaxResult();
        try {
            String username = selectDao.selectUserByid(id);
            List list = selectDao.selectCategory();
            if (list!=null){
                ar.setCode(1000);
                ar.setMsg("查询成功");
                ar.setData(username);
                ar.setCategory(list);

            }else {
                ar.setCode(1001);
                ar.setMsg("失败");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ar;
    }
    //通过cid找出分类后的图书
    public AjaxResult selectBookBycid(int cid, int catepage) {
        AjaxResult ar = new AjaxResult();
        catepage = (catepage - 1)*8;
        try {

            Long aLong = selectDao.selectBookBycid(cid);
            if (aLong%8!=0){
                ar.setMaxpage(aLong/8+1);
            }else {
                ar.setMaxpage(aLong/8);
            }
            List<Book> bookLimit = selectDao.selectBookLimit(cid, catepage);
            ar.setData(bookLimit);
            ar.setCode(1000);
//            ar.setMsg("分页成功");
            return ar;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
        //通过查找UId查找username
    public String selectUsernameByid(int uid) {
        try {
            String username = selectDao.selectUserByid(uid);
            return username;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
        //通过bid查书
    public Book selectBookBybid(int bid) {
        try {
            Book book = selectDao.selectBookBybid(bid);
            if (book!=null){
                return book;
            }else {
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
//  查找购物车
    public List<Car> selectCar(int uid) {
        try {
            List<Car> cars = selectDao.selectCarByuid(uid);
            if (cars!=null){
                for (Car car : cars) {
                    Book book = selectDao.selectBookBybid(car.getBid());
                    car.setBname(book.getBname());
                    car.setPrice(book.getPrice());
                    car.setImage(book.getImage());
                }
                return cars;
            }else {
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //通过UID查找订单
    public List<Orders> selectOrderByuid(int uid) {
        try {
            List<Orders> orders = selectDao.selectOrdersByuid(uid);
            Tb_user tbUser = selectDao.selectAllUserByuid(uid);
            for (Orders order : orders) {
                order.setPhone(tbUser.getPhone());
                order.setUsername(tbUser.getUsername());
                if (order.getState()==1){
                    order.setStringstate("未付款");
                }else if (order.getState()==2){
                    order.setStringstate("已付款未发货");
                }else if (order.getState()==3){
                    order.setStringstate("已发货未到货");
                }else if (order.getState()==4){
                    order.setStringstate("已收货");
                }
                List<Orderitem> orderitems = selectDao.selectorderitemByoid(order.getOid());
                int ordercount=0;
                for (Orderitem orderitem : orderitems) {
                    ordercount = ordercount+orderitem.getCount();
                }
                order.setOrderscount(ordercount);
            }
            return orders;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
//  通过oid查订单详情
    public List<Orderitem> selectOrderitemByoid(int oid) {
        try {
            List<Orderitem> orderitems = selectDao.selectorderitemByoid(oid);
            for (Orderitem orderitem : orderitems) {
                int bid = orderitem.getBid();
                Book book = selectDao.selectBookBybid(bid);
                orderitem.setBname(book.getBname());
                orderitem.setPrice(book.getPrice());

            }
            if (orderitems.size()!=0){
                return orderitems;
            }else {
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
//      查找所有订单
    public List<Orders> selectOrder() {
        try {
            List<Orders> orders = selectDao.selectOrders();
            for (Orders order : orders) {
                String username = selectDao.selectUserByid(order.getUid());
                order.setUsername(username);
                if (order.getState()==1){
                    order.setStringstate("未付款");
                }else if (order.getState()==2){
                    order.setStringstate("已付款未发货");
                }else if (order.getState()==3){
                    order.setStringstate("已发货未到货");
                }else if (order.getState()==4){
                    order.setStringstate("已收货");
                }
            }
            if (orders.size()!=0){
                return orders;
            }else {
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //查找所有订单详情
    public List<Orderitem> selectOrderitem() {
        try {
            List<Orderitem> orderitems = selectDao.selectOrderitem();
            for (Orderitem orderitem : orderitems) {
                Book book = selectDao.selectBookBybid(orderitem.getBid());
                orderitem.setBname(book.getBname());
                orderitem.setPrice(book.getPrice());
                orderitem.setAuthor(book.getAuthor());
                orderitem.setImage(book.getImage());
            }
            if (orderitems.size()!=0){
                return orderitems;
            }else {
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Tb_user selectUserByuid(int uid) {
        try {
            Tb_user tbUser = selectDao.selectAllUserByuid(uid);
            return tbUser;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
